﻿using System;
using System.Windows.Forms;
using SimpleGPSApp.Base;
using Microsoft.WindowsMobile.Samples.Location;

namespace SimpleGPSApp.Controls
{
    public partial class DistanceDisplay : UserControl
    {
        private DistanceType currentDisplay = DistanceType.Total;
        private DistanceType lastDisplay = DistanceType.FromStart;
        private DistanceCounter distanceCounter = new DistanceCounter();
        private const string kilometers = "km/h";
        private const string meters = "m";

        public DistanceDisplay()
        {
            InitializeComponent();
            ShowDistance();
        }

        public void CountDistance(GpsPosition position)
        {
            this.distanceCounter.CountDistance(position);
            ShowDistance();
        }

        private void ShowDistance()
        {
            switch (this.currentDisplay)
            {
                case DistanceType.Current:
                    UpdateControlValue(distanceCounter.CurrentDistance);
                    break;
                case DistanceType.FromStart:
                    UpdateControlValue(distanceCounter.DistanceFromStart);
                    break;
                case DistanceType.Total:
                    UpdateControlValue(Root.Statistics.TotalDistance);
                    break;
            }
        }

        private void UpdateControlValue(double value)
        {
            string unit = "m";
            if (value >= 1000)
            {
                value = value / 1000;
                unit = "km";
            }

            this.unitValueControl.Value = string.Format("{0:0.##}", value);
            this.unitValueControl.Unit = unit;
            this.unitValueControl.Refresh();
        }

        private void unitValueControl_Click(object sender, EventArgs e)
        {
            if (this.distanceCounter == null)
            {
                this.distanceCounter = new DistanceCounter();
            }

            if (currentDisplay == DistanceType.FromStart)
            {
                currentDisplay = DistanceType.Total;
                lastDisplay = DistanceType.FromStart;
                this.unitValueControl.ValueName = "Całkowita";
                UpdateControlValue(Root.Statistics.TotalDistance);
                return;
            }

            if (currentDisplay == DistanceType.Total & lastDisplay == DistanceType.FromStart)
            {
                currentDisplay = DistanceType.Current;
                lastDisplay = DistanceType.Total;
                this.unitValueControl.ValueName = "Aktualna";
                UpdateControlValue(distanceCounter.CurrentDistance);
                return;
            }

            if (currentDisplay == DistanceType.Current & lastDisplay == DistanceType.Total)
            {
                currentDisplay = DistanceType.FromStart;
                lastDisplay = DistanceType.Current;
                this.unitValueControl.ValueName = "Od startu";
                UpdateControlValue(distanceCounter.DistanceFromStart);
            }
        }

        private enum DistanceType
        {
            Total,
            FromStart,
            Current,
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            this.distanceCounter.ResetCurrentDistance();
            this.UpdateControlValue(0);
        }

        private void button_startStop_Click(object sender, EventArgs e)
        {
            if (!this.distanceCounter.CountDistanceOn)
            {
                this.distanceCounter.CountDistanceOn = true;
                this.button_startStop.Text = "Stop";
            }
            else
            {
                this.distanceCounter.CountDistanceOn = false;
                this.button_startStop.Text = "Start";
            }
        }
    }
}
